home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / umich / utils / bison.arc / READER.C < prev    next >
C/C++ Source or Header  |  1988-07-11  |  35KB  |  1,699 lines

  1. /* Input parser for bison
  2.    Copyright (C) 1984, 1986 Bob Corbett and Free Software Foundation, Inc.
  3.  
  4. BISON is distributed in the hope that it will be useful, but WITHOUT ANY
  5. WARRANTY.  No author or distributor accepts responsibility to anyone
  6. for the consequences of using it or for whether it serves any
  7. particular purpose or works at all, unless he says so in writing.
  8. Refer to the BISON General Public License for full details.
  9.  
  10. Everyone is granted permission to copy, modify and redistribute BISON,
  11. but only under the conditions described in the BISON General Public
  12. License.  A copy of this license is supposed to have been given to you
  13. along with BISON so you can know your rights and responsibilities.  It
  14. should be in a file named COPYING.  Among other things, the copyright
  15. notice and this notice must be preserved on all copies.
  16.  
  17.  In other words, you are welcome to use, share and improve this program.
  18.  You are forbidden to forbid anyone else to use, share and improve
  19.  what you give them.   Help stamp out software-hoarding!  */
  20.  
  21. /* read in the grammar specification and record it in the format described in gram.h.
  22.   All guards are copied into the fguard file and all actions into faction,
  23.   in each case forming the body of a C function (yyguard or yyaction)
  24.   which contains a switch statement to decide which guard or action to execute.
  25.  
  26. The entry point is reader().  */
  27.  
  28. #include <stdio.h>
  29. #include <ctype.h>
  30. #include "files.h"
  31. #include "new.h"
  32. #include "symtab.h"
  33. #include "lex.h"
  34. #include "gram.h"
  35.  
  36.  
  37. #define    LTYPESTR    "\n#ifndef YYLTYPE\ntypedef\n  struct yyltype\n\
  38.     {\n      int timestamp;\n      int first_line;\n      int first_column;\n\
  39.       int last_line;\n      int last_column;\n      char *text;\n   }\n\
  40.   yyltype;\n\n#define YYLTYPE yyltype\n#endif\n\n"
  41.  
  42. /* Number of slots allocated (but not necessarily used yet) in `rline'  */
  43. int rline_allocated;
  44.  
  45. extern int definesflag;
  46. extern bucket *symval;
  47. extern int numval;
  48. extern int failure;
  49. extern int expected_conflicts;
  50.  
  51. typedef
  52.   struct symbol_list
  53.     {
  54.       struct symbol_list *next;
  55.       bucket *sym;
  56.       bucket *ruleprec;
  57.     }
  58.   symbol_list;
  59.  
  60.  
  61.  
  62. int lineno;
  63. bucket *symval;
  64. symbol_list *grammar;
  65. int start_flag;
  66. bucket *startval;
  67. char **tags;
  68.  
  69. static int typed;  /* nonzero if %union has been seen.  */
  70.  
  71. static int lastprec;  /* incremented for each %left, %right or %nonassoc seen */
  72.  
  73. static int gensym_count;  /* incremented for each generated symbol */
  74.  
  75. static bucket *errtoken;
  76.  
  77. reader()
  78. {
  79.  
  80.   start_flag = 0;
  81.   startval = NULL;  /* start symbol not specified yet. */
  82.  
  83.   translations = 0;  /* initially assume token number translation not needed.  */
  84.  
  85.   nsyms = 1;
  86.   nvars = 0;
  87.   nrules = 0;
  88.   nitems = 0;
  89.   rline_allocated = 10;
  90.   rline = NEW2(rline_allocated, short);
  91.  
  92.   typed = 0;
  93.   lastprec = 0;
  94.  
  95.   gensym_count = 0;
  96.  
  97.   semantic_parser = 0;
  98.   pure_parser = 0;
  99.  
  100.   grammar = NULL;
  101.  
  102.   init_lex();
  103.   lineno = 1;
  104.  
  105.   /* initialize the symbol table.  */
  106.   tabinit();
  107.   /* construct the error token */
  108.   errtoken = getsym("error");
  109.   errtoken->class = STOKEN;
  110.   /* construct a token that represents all undefined literal tokens. */
  111.   /* it is always token number 2.  */
  112.   getsym("$illegal.")->class = STOKEN;
  113.   /* Read the declaration section.  Copy %{ ... %} groups to ftable and fdefines file.
  114.      Also notice any %token, %left, etc. found there.  */
  115.   fprintf(ftable, "\n/*  A Bison parser, made from %s  */\n\n", infile);
  116.   read_declarations();
  117.   /* output the definition of YYLTYPE into the fattrs and fdefines files.  */
  118.   output_ltype();
  119.   /* start writing the guard and action files, if they are needed.  */
  120.   output_headers();
  121.   /* read in the grammar, build grammar in list form.  write out guards and actions.  */
  122.   readgram();
  123.   /* write closing delimiters for actions and guards.  */
  124.   output_trailers();
  125.   /* assign the symbols their symbol numbers.
  126.      Write #defines for the token symbols into fdefines if requested.  */
  127.   packsymbols();
  128.   /* convert the grammar into the format described in gram.h.  */
  129.   packgram();
  130.   /* free the symbol table data structure
  131.      since symbols are now all referred to by symbol number.  */
  132.   free_symtab();
  133. }
  134.  
  135.  
  136.  
  137. /* read from finput until %% is seen.  Discard the %%.
  138. Handle any % declarations,
  139. and copy the contents of any %{ ... %} groups to fattrs.  */
  140.  
  141. read_declarations ()
  142. {
  143.   register int c;
  144.   register int tok;
  145.  
  146.   for (;;)
  147.     {
  148.       c = skip_white_space();
  149.  
  150.       if (c == '%')
  151.     {
  152.       tok = parse_percent_token();
  153.  
  154.       switch (tok)
  155.         {
  156.         case TWO_PERCENTS:
  157.           return;
  158.  
  159.         case PERCENT_LEFT_CURLY:
  160.           copy_definition();
  161.           break;
  162.  
  163.         case TOKEN:
  164.           parse_token_decl (STOKEN, SNTERM);
  165.           break;
  166.     
  167.         case NTERM:
  168.           parse_token_decl (SNTERM, STOKEN);
  169.           break;
  170.     
  171.         case TYPE:
  172.           parse_type_decl();
  173.           break;
  174.     
  175.         case START:
  176.           parse_start_decl();
  177.           break;
  178.     
  179.         case UNION:
  180.           parse_union_decl();
  181.           break;
  182.     
  183.         case EXPECT:
  184.           parse_expect_decl();
  185.           break;
  186.     
  187.         case LEFT:
  188.           parse_assoc_decl(LEFT_ASSOC);
  189.           break;
  190.  
  191.         case RIGHT:
  192.           parse_assoc_decl(RIGHT_ASSOC);
  193.           break;
  194.  
  195.         case NONASSOC:
  196.           parse_assoc_decl(NON_ASSOC);
  197.           break;
  198.  
  199.         case SEMANTIC_PARSER:
  200.           semantic_parser = 1;
  201.           open_extra_files();
  202.           break;
  203.  
  204.         case PURE_PARSER:
  205.           pure_parser = 1;
  206.           break;
  207.  
  208.         default:
  209.           fatal("junk after % in definition section");
  210.         }
  211.     }
  212.       else if (c == EOF)
  213.         fatal("no input grammar");
  214.       else/* JF changed msg */
  215.         fatals("Unrecognized char '%c' in declaration section",c);
  216.  
  217.     }
  218. }
  219.  
  220.  
  221. /* copy the contents of a %{ ... %} into the definitions file.
  222. The %{ has already been read.  Return after reading the %}.  */
  223. copy_definition ()
  224. {
  225.   register int c;
  226.   register int match;
  227.   register int ended;
  228.   register int after_percent;  /* -1 while reading a character if prev char was % */
  229.  
  230.   fprintf(fattrs, "#line %d \"%s\"\n", lineno, infile);
  231.  
  232.   after_percent = 0;
  233.  
  234.   c = getc(finput);
  235.  
  236.   for (;;)
  237.     {
  238.       switch (c)
  239.     {
  240.     case '\n':
  241.       putc(c, fattrs);
  242.       lineno++;
  243.       break;
  244.  
  245.     case '%':
  246.           after_percent = -1;
  247.       break;
  248.           
  249.     case '\'':
  250.     case '"':
  251.       match = c;
  252.       putc(c, fattrs);
  253.       c = getc(finput);
  254.  
  255.       while (c != match)
  256.         {
  257.           if (c == EOF || c == '\n')
  258.         fatal("unterminated string");
  259.  
  260.           putc(c, fattrs);
  261.           
  262.           if (c == '\\')
  263.         {
  264.           c = getc(finput);
  265.           if (c == EOF || c == '\n')
  266.             fatal("unterminated string");
  267.           putc(c, fattrs);
  268.           if (c == '\n')
  269.             lineno++;
  270.         }
  271.  
  272.           c = getc(finput);
  273.         }
  274.  
  275.       putc(c, fattrs);
  276.       break;
  277.  
  278.     case '/':
  279.       putc(c, fattrs);
  280.       c = getc(finput);
  281.       if (c != '*')
  282.         continue;
  283.  
  284.       putc(c, fattrs);
  285.       c = getc(finput);
  286.  
  287.       ended = 0;
  288.       while (!ended)
  289.         {
  290.           if (c == '*')
  291.         {
  292.           while (c == '*')
  293.             {
  294.               putc(c, fattrs);
  295.               c = getc(finput);
  296.             }
  297.  
  298.           if (c == '/')
  299.             {
  300.               putc(c, fattrs);
  301.               ended = 1;
  302.             }
  303.         }
  304.           else if (c == '\n')
  305.         {
  306.           lineno++;
  307.           putc(c, fattrs);
  308.           c = getc(finput);
  309.         }
  310.           else if (c == EOF)
  311.         fatal("unterminated comment in %{ definition");
  312.           else
  313.         {
  314.           putc(c, fattrs);
  315.           c = getc(finput);
  316.         }
  317.         }
  318.  
  319.       break;
  320.  
  321.     case EOF:
  322.       fatal("unterminated %{ definition");
  323.  
  324.     default:
  325.       putc(c, fattrs);
  326.     }
  327.  
  328.       c = getc(finput);
  329.  
  330.       if (after_percent)
  331.     {
  332.       if (c == '}')
  333.         return;
  334.       putc('%', fattrs);
  335.     }
  336.       after_percent = 0;
  337.  
  338.     }
  339.  
  340. }
  341.  
  342.  
  343.  
  344. /* parse what comes after %token or %nterm.
  345. For %token, what_is is STOKEN and what_is_not is SNTERM.
  346. For %nterm, the arguments are reversed.  */
  347.  
  348. parse_token_decl (what_is, what_is_not)
  349.      int what_is, what_is_not;
  350. {
  351. /*   register int start_lineno; JF */
  352.   register int token = 0;
  353.   register int prev;
  354.   register char *typename = 0;
  355.   int k;
  356.   extern char token_buffer[];
  357.  
  358. /*   start_lineno = lineno; JF */
  359.  
  360.   for (;;)
  361.     {
  362.       if(ungetc(skip_white_space(), finput) == '%')
  363.     return;
  364.  
  365. /*      if (lineno != start_lineno)
  366.     return; JF */
  367.  
  368.       /* we have not passed a newline, so the token now starting is in this declaration */
  369.       prev = token;
  370.  
  371.       if ((token = lex()) == TYPENAME)
  372.     {
  373.       k = strlen(token_buffer);
  374.       if (typename) free (ty